'use strict';
// 获取操作类型
const { ACTION_TYPE } = require('../constants/actionType');
// 获取Controller类
const Controller = require('egg').Controller;

class PictureController extends Controller {
  async upload() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { num, picIds } = await ctx.service.picture.upload(userId);
    await ctx.service.record.create(userId, ACTION_TYPE.UploadPicture, `照片数量：${num}`);
    ctx.body = ctx.helper.formatResponse({ num, picIds }, 200, '上传照片成功');
  }

  async move() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { albumId, picIds } = ctx.request.body;
    const success = await ctx.service.picture.move(albumId, JSON.parse(picIds));
    await ctx.service.record.create(userId, ACTION_TYPE.MovePicture, `照片数量：${picIds.length}`);
    ctx.body = ctx.helper.formatResponse({ success }, 200, '移动照片成功');
  }

  async destory() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { picIds } = ctx.request.query;
    const success = await ctx.service.picture.destory(userId, JSON.parse(picIds));
    await ctx.service.record.create(userId, ACTION_TYPE.UploadPicture, `照片数量：${picIds.length}`);
    ctx.body = ctx.helper.formatResponse({ success }, 200, '删除照片成功');
  }

  async index() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { page, size } = ctx.request.query;
    const { list, total } = await ctx.service.picture.index(userId, page, size);
    ctx.body = ctx.helper.formatResponse({ list: ctx.helper.jsonToHump(list), total }, 200, '获取所有照片成功');
  }

  async albumIndex() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { albumId, page, size } = ctx.request.query;
    const { list, total } = await ctx.service.picture.albumIndex(userId, albumId, page, size);
    ctx.body = ctx.helper.formatResponse({ list: ctx.helper.jsonToHump(list), total }, 200, '获取相册中照片成功');
  }

  async searchByText() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { text } = ctx.request.query;
    const { list } = await ctx.service.picture.searchByText(userId, text);
    ctx.body = ctx.helper.formatResponse({ list: ctx.helper.jsonToHump(list), total: list.length }, 200, '文字搜索照片成功');
  }

  async searchByTag() {
    const { ctx } = this;
    const { userId } = ctx.helper.jsonToHump(ctx.decode);
    const { tagId, page, size } = ctx.request.query;
    const { list, total } = await ctx.service.picture.searchByTag(userId, tagId, page, size);
    ctx.body = ctx.helper.formatResponse({ list: ctx.helper.jsonToHump(list), total }, 200, '标签搜索照片成功');
  }

}

module.exports = PictureController;
